import datetime

from django.conf.urls.defaults import *
from django.views.generic.list_detail import object_list, object_detail

import views
from models import Post, Forum, Thread

forum_list = Forum.objects.all()
total_threads = 0
total_posts = 0
for forum in forum_list:
    total_threads += forum.threads
    total_posts += forum.posts

forum_dict = {
    'queryset': Forum.objects.all(),
    'template_name': 'forum/home.html',
    'extra_context': {
        'total_threads': total_threads, 
        'total_posts': total_posts,
    }
}

start_time  = datetime.datetime.now() - datetime.timedelta(1)
recent_dict = {
    'queryset': Thread.objects.filter(thread_latest_post__time__gt=start_time).select_related(depth=1).order_by('-thread_latest_post'),
    'template_name': 'forum/recent.html',
    'template_object_name': 'thread',
    'allow_empty': 'true',
    'paginate_by': 20,
    'extra_context': {
        'total_threads': Thread.objects.filter(thread_latest_post__time__gt=start_time).count(),
    }
}

urlpatterns = patterns('',
    url(r'^$',                                                                      object_list, dict(forum_dict),   name='forum-home'),
    url(r'^recent/$',                                                               object_list, dict(recent_dict),  name='forum-recent'),
    url(r'^watched/$',                                                              views.watched,          name='forum-watched'),
    url(r'^(?P<forum>[A-Za-z0-9-_]+)/$',                                            views.forum,            name='forum-forum'),
    url(r'^(?P<forum>[A-Za-z0-9-_]+)/(?P<thread>[0-9]+)/$',                         views.thread,           name='forum-thread'),
    url(r'^(?P<forum>[A-Za-z0-9-_]+)/new/$',                                        views.thread_new,       name='forum-thread-new'),
    url(r'^(?P<forum>[A-Za-z0-9-_]+)/(?P<thread>[0-9]+)/watch/$',                   views.thread_watch,     name='forum-thread-watch'),
    url(r'^(?P<forum>[A-Za-z0-9-_]+)/(?P<thread>[0-9]+)/unwatch/$',                 views.thread_unwatch,   name='forum-thread-unwatch'),
    url(r'^(?P<forum>[A-Za-z0-9-_]+)/(?P<thread>[0-9]+)/reply/$',                   views.post_new,         name='forum-post-new'),
    url(r'^(?P<forum>[A-Za-z0-9-_]+)/(?P<thread>[0-9]+)/(?P<post>[0-9]+)/quote/$',  views.post_quote,       name='forum-post-quote'),
    url(r'^(?P<forum>[A-Za-z0-9-_]+)/(?P<thread>[0-9]+)/(?P<post>[0-9]+)/edit/$',   views.post_edit,        name='forum-post-edit'),
    url(r'^(?P<forum>[A-Za-z0-9-_]+)/(?P<thread>[0-9]+)/(?P<post>[0-9]+)/delete/$', views.post_delete,      name='forum-post-delete'),
)
